using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Profile;

namespace Hydra.Web.Administration
{
    public partial class ManageUsers : Page
    {
        private MembershipUserCollection allRegisteredUsers = Membership.GetAllUsers();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                lblOnlineUsers.Text = Membership.GetNumberOfUsersOnline().ToString();
                lblTotalUsers.Text = allRegisteredUsers.Count.ToString();
                string[] alph = "A;B;C;D;E;F;G;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z;All".Split(';');
                rptAlphabetBar.DataSource = alph;
                rptAlphabetBar.DataBind();
            }
        }
        protected void rptAlphabetBar_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            gvUsers.Attributes.Add("SearchByEmail", false.ToString());
            if (e.CommandArgument.ToString().Length == 1)
            {
                gvUsers.Attributes.Add("SearchText", e.CommandArgument.ToString() + "%");
                this.BindAllUsers(false);
            }
            else
            {
                gvUsers.Attributes.Add("SearchText", "");
                this.BindAllUsers(false);
            }
        }
        private void BindAllUsers(bool reloadAllUsers)
        {
            MembershipUserCollection allUsers = null;
            if (reloadAllUsers)
                allUsers = Membership.GetAllUsers();

            string searchText = "";
            if (!string.IsNullOrEmpty(gvUsers.Attributes["SearchText"]))
                searchText = gvUsers.Attributes["SearchText"];
            bool searchByEmail = false;
            if (!string.IsNullOrEmpty(gvUsers.Attributes["SearchByEmail"]))
                searchByEmail = bool.Parse(gvUsers.Attributes["SearchByEmail"]);
            if (searchText.Length > 0)
            {
                if (searchByEmail)
                    allUsers = Membership.FindUsersByEmail(searchText);
                else
                    allUsers = Membership.FindUsersByName(searchText);
            }
            else
            {
                allUsers = allRegisteredUsers;
            }
            gvUsers.DataSource = allUsers;
            gvUsers.DataBind();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            bool searchByEmail = (ddlUserSearchTypes.SelectedValue == "E-mail");
            gvUsers.Attributes.Add("SearchText", "%" + txtSearchText.Text + "%");
            gvUsers.Attributes.Add("SearchByEmail", searchByEmail.ToString());
            BindAllUsers(false);
        }
        protected void gvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string userName = gvUsers.DataKeys[e.RowIndex].Value.ToString();
            ProfileManager.DeleteProfile(userName);
            Membership.DeleteUser(userName);
            BindAllUsers(true);
            lblTotalUsers.Text = allRegisteredUsers.Count.ToString();
        }
        protected void gvUsers_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton btn = e.Row.Cells[6].Controls[0] as ImageButton;
                btn.OnClientClick = "if (confirm('Are you sure you want to delete this user?') == false) return false;";
            }
        }



    }
}